import { ProductStocks } from './productStock';
let _ProductStocks = ProductStocks;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import ProductStockMock from './mallm/product/productStockMock';
 * ProductStockMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/product/productStock/listKey').reply(config => {
      let params = config.params;
      let mockProductStocks = _ProductStocks.filter(productStock => {
      	if(params.id||params.id==''){ if( productStock.id.indexOf(params.id)>=0) return true;}
      	if(params.goodsId||params.goodsId==''){ if( productStock.goodsId.indexOf(params.goodsId)>=0) return true;}
      	if(params.goodsSpecificationIds||params.goodsSpecificationIds==''){ if( productStock.goodsSpecificationIds.indexOf(params.goodsSpecificationIds)>=0) return true;}
      	if(params.goodsSn||params.goodsSn==''){ if( productStock.goodsSn.indexOf(params.goodsSn)>=0) return true;}
      	if(params.goodsNumber||params.goodsNumber==''){ if( productStock.goodsNumber.indexOf(params.goodsNumber)>=0) return true;}
      	if(params.retailPrice||params.retailPrice==''){ if( productStock.retailPrice.indexOf(params.retailPrice)>=0) return true;}
      	if(params.wholesalePrice||params.wholesalePrice==''){ if( productStock.wholesalePrice.indexOf(params.wholesalePrice)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( productStock.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockProductStocks
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/product/productStock/list').reply(config => {
      let params = config.params;
      let mockProductStocks = _ProductStocks.filter(productStock => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(productStock.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsId||params.goodsId==''){if(productStock.goodsId.indexOf(params.goodsId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsSpecificationIds||params.goodsSpecificationIds==''){if(productStock.goodsSpecificationIds.indexOf(params.goodsSpecificationIds)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsSn||params.goodsSn==''){if(productStock.goodsSn.indexOf(params.goodsSn)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsNumber||params.goodsNumber==''){if(productStock.goodsNumber.indexOf(params.goodsNumber)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.retailPrice||params.retailPrice==''){if(productStock.retailPrice.indexOf(params.retailPrice)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.wholesalePrice||params.wholesalePrice==''){if(productStock.wholesalePrice.indexOf(params.wholesalePrice)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(productStock.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockProductStocks
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockProductStocks.length;
      mockProductStocks = mockProductStocks.filter((productStock, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockProductStocks,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/product/productStock/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _ProductStocks = _ProductStocks.filter(productStock => productStock.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/product/productStock/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _ProductStocks = _ProductStocks.filter(productStock => !params.includes(productStock.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/product/productStock/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,goodsId,goodsSpecificationIds,goodsSn,goodsNumber,retailPrice,wholesalePrice,deptid } = params;
      _ProductStocks.some(productStock => {
        if (productStock.id === id) {
			productStock.id = id;
			productStock.goodsId = goodsId;
			productStock.goodsSpecificationIds = goodsSpecificationIds;
			productStock.goodsSn = goodsSn;
			productStock.goodsNumber = goodsNumber;
			productStock.retailPrice = retailPrice;
			productStock.wholesalePrice = wholesalePrice;
			productStock.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/product/productStock/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,goodsId,goodsSpecificationIds,goodsSn,goodsNumber,retailPrice,wholesalePrice,deptid } = params;
      _ProductStocks.push({
		id:id,
		goodsId:goodsId,
		goodsSpecificationIds:goodsSpecificationIds,
		goodsSn:goodsSn,
		goodsNumber:goodsNumber,
		retailPrice:retailPrice,
		wholesalePrice:wholesalePrice,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};